package com.example.shengyin.controller;

import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.Team;
import com.example.shengyin.service.TeamService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/team")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("团队")
public class TeamController {

    @Autowired
    private TeamService service;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/search")
    @ApiOperation("分页查找")
    public Result team(@ApiParam(name = "tname",value = "团队名") @RequestParam(required = false) String tname,
                       @RequestParam(name = "page", defaultValue= "1") int page,
                       @RequestParam(name = "limit", defaultValue = "10") int limit) {
        Page p = service.selectList(tname, page, limit);
        int count = service.find(tname);
        if(count==0||p==null)
            return Result.error(null);
        return Result.success(p);
    }

    @PostMapping("/add")
    @ApiOperation("新增")
    public Result add(@Valid Team team, HttpServletRequest request){
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        System.out.println("<<<<<<<<<"+team );
        team.setPoster(admin.getUname());
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
        team.setPostTime(timestamp);
        int r = service.add(team);
        if (r > 0)
            return Result.success("新增成功");
        return Result.error("新增失败");
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public Result update(Team team)  {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
        team.setPostTime(timestamp);
        int r = service.update(team);
        if (r > 0)
            return Result.success("修改成功");
        return Result.error("修改失败");
    }

    @DeleteMapping("/deleteByIds/{ids}")
    @ApiOperation("删除")
    public Result delete(@NotNull(message = "编号不能为空")@PathVariable(value = "ids")String ids){
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.delByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("连删除")
    public Result delete(@PathVariable(value = "id")Long id)
    {
        int delete = service.delById(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @ApiOperation("查找")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        Team select = service.selectById(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }

    @ApiOperation("查找")
    @GetMapping(value = "/findAll")
    public Result findAll()
    {
        List<Team> all = service.findAll();
        return all==null ? Result.error("无此团队信息"):new Result(0,all,"查找成功");
    }
    @ApiOperation("修改状态按钮")
    @GetMapping("/isvalue/{id}/{isvalue}")
    public Result updateIsvalid(@PathVariable(value = "id")Long id, @PathVariable(value = "isvalue")Boolean isvalue,HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }

        int symtype = service.updateIsvalid(isvalue, id);
        if (symtype>0) {
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }

    }
}
